package com.sinopec.yelofixedreader.uhf.uhfdi

import androidx.lifecycle.LifecycleOwner
import com.rscja.deviceapi.RFIDWithUHFA4NetWork
import com.sinopec.iuhf.IUHFSettings
import com.sinopec.iuhf.IUhfHelper
import com.sinopec.uhf.chengwei.lifecycleUhfHelper
import com.sinopec.uhf.chengwei.net.NetChengWeiSettings
import com.sinopec.uhf.chengwei.net.netLifecycleUhfHelper
import com.sinopec.uhf.chengwei.settings.ChengWeiSettings
import com.sinopec.uhf.settings.SettingList
import com.sinopec.uhf.initrc.lifecycleUhfHelper as old

class IUHFHelperDi {

    data class Device(
        val host: String = "",
        val port: String = "",
    ) {

        companion object {
            val 罗丹贝尔 = Device()
            val 成为主机模式 = Device()
            var 成为网络模式 = Device("10.0.72.37","9160")


            var current = 成为主机模式
        }

    }

    val uhfA4 by lazy { RFIDWithUHFA4NetWork() }

    fun uhf(
        lifecycleOwner: LifecycleOwner,
        device: Device,
    ): IUhfHelper {
        return when {
            device === Device.罗丹贝尔 -> {
                lifecycleOwner.old
            }
            device === Device.成为主机模式 -> {
                lifecycleOwner.lifecycleUhfHelper
            }
            device === Device.成为网络模式 -> {
                lifecycleOwner.netLifecycleUhfHelper(
                    uhfA4,
                    device.host,
                    device.port
                )
            }
            else -> {
                error("无法识别设备")
            }
        }
    }

    fun settings(
        device: Device,
        //just for init
        iUhfHelper: IUhfHelper
    ): IUHFSettings {
        return if (device === Device.罗丹贝尔){
            SettingList
        }else if (device === Device.成为主机模式){
            ChengWeiSettings
        }else if (device === Device.成为网络模式){
            NetChengWeiSettings(uhfA4)
        }else{
            error("无法识别设备")
        }
    }

}

operator fun IUHFHelperDi.invoke(
    lifecycleOwner: LifecycleOwner,
    device: IUHFHelperDi.Device,
) = uhf(lifecycleOwner, device)